import 'dart:convert';

import 'package:azlistview/azlistview.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/http/dio_service.dart';
import 'package:flutter_ncsjqz_getx/model/im_users_list_entity.dart';
import 'package:get/get.dart';
import 'package:lpinyin/lpinyin.dart';

class AddressListLogic extends GetxController {
  bool showIcon = true;
  TextEditingController textEditingControllerSearch = TextEditingController();
  List<ContactInfo> contacts = []; // 列表数据
  List<ContactInfo> searchContacts = []; // 搜索的结果 从列表数据中检索得到
  double susItemHeight = 40;
  int pageIndex = 0;
  int pageSize = 50;
  int pages = 0;
  List<String?> friendIds = [];
  List<ContactInfo?> users = [];
  bool allUser = false; // 全选联系人
  bool fansAllUser = false; // 所以关注的人
  List<ImUsersListData>? imUsersListData = [];

  /// 获取联系人数据
  void loadData() async {
    ImUsersListEntity data = await DioService<ImUsersListEntity>().friendList();
    if(data.code == 0){
      imUsersListData = data.data;
      data.data!.forEach((element) {
        contacts.add(ContactInfo(
          name: element.friendName!,
          friendId: element.friendId,
          ryUserId: element.ryUserId,
          img: element.picUrl,
        ),
        );
      });
      handleList(contacts);
    }
  }

  /// 组装数据
  void handleList(List<ContactInfo> list) {
    if (list.isEmpty) return;
    for (int i = 0, length = list.length; i < length; i++) {
      String pinyin = PinyinHelper.getPinyinE(list[i].name);
      String tag = pinyin.substring(0, 1).toUpperCase();
      list[i].namePinyin = pinyin;
      if (RegExp("[A-Z]").hasMatch(tag)) {
        list[i].tagIndex = tag;
      } else {
        list[i].tagIndex = "#";
      }
    }
    // A-Z sort.
    SuspensionUtil.sortListBySuspensionTag(searchContacts.length > 0 ?searchContacts : contacts);

    // 副标题
    SuspensionUtil.setShowSuspensionStatus(searchContacts.length > 0 ?searchContacts : contacts);

    // 头部
    // contacts.insert(0, ContactInfo(name: 'header', tagIndex: '↑'));
    update();
  }

  /// 全选
  setAllUser(){
    allUser = !allUser;
    if(allUser){
      friendIds = [];
      users = [];
      contacts.forEach((element) => friendIds.add(element.ryUserId));
      contacts.forEach((element) => users.add(element));
    }else{
      friendIds = [];
      users = [];
    }
    update();
  }

  /// 关注的人
  setFansUser(){
    fansAllUser = !fansAllUser;
    update();
  }
}

/// 通讯录model
class ContactInfo extends ISuspensionBean {
  String name;
  String? tagIndex;
  String? namePinyin;

  Color? bgColor;
  IconData? iconData;

  String? img;
  dynamic? ryUserId;
  String? firstletter;
  dynamic? friendId;
  ContactInfo({
    required this.name,
    this.tagIndex,
    this.namePinyin,
    this.bgColor,
    this.iconData,
    this.img,
    this.ryUserId,
    this.firstletter,
    this.friendId
  });

  ContactInfo.fromJson(Map<String, dynamic> json)
      : name = json['name'],
        img = json['img'],
        ryUserId = json['ryUserId'],
        friendId = json['userId'],
        firstletter = json['firstletter'];

  Map<String, dynamic> toJson() => {
       'ryUserId': ryUserId,
    'userId': friendId,
    'name': name,
    'img': img,
       'firstletter': firstletter,
       'tagIndex': tagIndex,
       'namePinyin': namePinyin,
       'isShowSuspension': isShowSuspension
  };

  @override
  String getSuspensionTag() => tagIndex!;

  @override
  String toString() => json.encode(this);
}
